<template>
  <div class="register-container">
    <el-card class="register-box" shadow="hover">
      <h3 style="text-align: center; margin-bottom: 20px;">不安全注册演示（明文存储密码）</h3>
      <el-form :model="form" :rules="rules" ref="registerForm" label-width="80px">
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入账号" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="form.confirmPassword" placeholder="请再次输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit" :loading="loading" style="width: 100%">注 册</el-button>
        </el-form-item>
        <div style="text-align: center;">
          <span>已有账号？</span>
          <el-button type="text" @click="goToUnsafeLogin">不安全登录</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import request from '@/utils/request';

export default {
  name: 'UserUnsafeRegister',
  data() {
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    };
    
    return {
      loading: false,
      form: {
        account: '',
        password: '',
        confirmPassword: ''
      },
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 20, message: '账号长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少 6 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs.registerForm.validate(valid => {
        if (!valid) return;

        this.loading = true;
        // 仅传递account和password，不传递confirmPassword
        const userData = {
          account: this.form.account,
          password: this.form.password
        };
        
        request.post('/User_unsafe_register', userData)
          .then(res => {
            this.loading = false;
            if (res.data.code === 1) {
              this.$message.success('注册成功！(警告：密码以明文形式存储在数据库中)');
              this.$router.push('/user_unsafe_login');
            } else {
              this.$message.error(res.data.msg || '注册失败');
            }
          })
          .catch(err => {
            this.loading = false;
            this.$message.error('网络错误，请检查服务是否启动');
            console.error(err);
          });
      });
    },
    goToUnsafeLogin() {
      this.$router.push('/user_unsafe_login');
    }
  }
};
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #ff6b6b 0%, #ee5a6f 100%);
}

.register-box {
  width: 400px;
  padding: 40px;
  border-radius: 10px;
}

.el-form-item {
  margin-bottom: 20px;
}
</style>